"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能测试
Case Name   : 执行to before drop闪回操作前对同一张表样执行两次删表操作
Create At   : 2022/10/27
@zou_jialiang0501120111
Description :
    1.修改回收站参数enable_recyclebin为on
    2.创建表样并在表样内插入数据,并删除表样
    3.再次建表，表名称与被删除表名称一致，插入不同数据后二次删除表样
    4.执行to before drop闪回
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.闪回到第一次drop的表样
    5.成功
History     :
    Modified by @daiguatutu 2022/11/22:增加清理回收站步骤,
    同步调整清理环境的断言顺序
    Modified by @zou_jialiang050 2023/04/14:与开发沟通,step4闪回结果只能恢复到最近一次
    删除结果,修改断言
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class SQL(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Constant = Constant()
        self.Common = Common()
        self.table_name = 't_timecapsule_table_0002'
        self.default_value = \
            self.Common.show_param('enable_recyclebin')

    def test_timecapsule_table(self):
        step1 = '--step1:修改enable_recyclebin为on;expect:成功--'
        self.log.info(step1)
        mod_msg = self.primary_sh.execute_gsguc('set',
                                                self.Constant.GSGUC_SUCCESS_MSG,
                                                'enable_recyclebin =on')
        self.log.info(mod_msg)
        self.assertTrue(mod_msg, "执行失败" + step1)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        mod_value = self.Common.show_param('enable_recyclebin')
        self.log.info(mod_value)
        self.assertEquals(mod_value, 'on', "执行失败" + step1)

        step2 = '--step2:创建表样并在表样内插入数据;expect:成功--'
        self.log.info(step2)
        sql_cmd = f'''drop table if exists {self.table_name};
                      create table {self.table_name}
                      (
                         r_reason_sk    integer,
                         r_reason_id    character(16),
                         r_reason_desc  character(100)
                      )with (storage_type=ustore);
                     insert into {self.table_name} values (1, 'aa', 'reason1'),
                     (2, 'ab', 'reason2'),(3, 'ac', 'reason3');
                     drop table if exists {self.table_name};'''
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, result,
                      "执行失败" + step2)
        self.assertEquals(2, result.count(self.Constant.TABLE_DROP_SUCCESS),
                          '执行失败' + step2)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, result,
                      '执行失败' + step2)

        step3 = '--step3:再次建表，表名称与被删除表名称一致，插入不同数据后二次删除表样;expect:成功--'
        self.log.info(step3)
        sql_cmd = f'''drop table if exists {self.table_name};
                      create table {self.table_name}
                      (
                         r_reason_sk    integer,
                         r_reason_id    character(16),
                         r_reason_desc  character(100)
                      )with (storage_type=ustore);
                     insert into {self.table_name} values (4, 'aa', 'reason4'),
                     (5, 'ab', 'reason5'),(6, 'ac', 'reason6');
                     drop table if exists {self.table_name};'''
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, result,
                      "执行失败" + step3)
        self.assertEquals(2, result.count(self.Constant.TABLE_DROP_SUCCESS),
                          '执行失败' + step3)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, result, '执行失败' + step3)

        step4 = '--step4:执行to before drop表样闪回并查看表内数据;expect:表样闪回到第一次被删除的模式--'
        self.log.info(step4)
        sql_cmd = f'''timecapsule table {self.table_name} to before drop;
                      select * from {self.table_name};'''
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn('TimeCapsule Table', result, '执行失败' + step4)
        self.assertNotIn('reason1', result, '执行失败' + step4)
        self.assertIn('reason4', result, '执行失败' + step4)

    def tearDown(self):
        step5 = '--step5:清理环境;expect:成功--'
        self.log.info(step5)
        clean_environment = self.primary_sh.execut_db_sql(
                            f'''drop table {self.table_name};
                                purge recyclebin;''')
        self.log.info(clean_environment)
        mod_msg = self.primary_sh.execute_gsguc('set',
                                                self.Constant.GSGUC_SUCCESS_MSG,
                                                f'enable_recyclebin ='
                                                f'{self.default_value}')
        self.log.info(mod_msg)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.primary_sh.get_db_cluster_status()
        self.assertIn(self.Constant.DROP_TABLE_SUCCESS, clean_environment,
                      "执行失败" + step5)
        self.assertIn('PURGE RECYCLEBIN', clean_environment,
                      "执行失败" + step5)
        self.assertTrue(mod_msg, "执行失败" + step5)
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + step5)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

